package ua.kpi_java_training.nikishov.module1;

import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;

import static org.junit.Assert.assertEquals;

/**
 * Test for LongDivision class.
 *
 * @author Alexey Nikishov.
 */
public class LongDivisionTest {
    @Before
    public void setUp() {
        LongDivision.divDigits.clear();
    }

    /**
     * Test of a method that splits specified number to digits.
     */
    @Test
    public void testSplitDivIntoDigits() {
        ArrayList<Integer> expectedDigits = new ArrayList<Integer>(
                Arrays.asList(1, 2, 3, 4, 5)
        );
        ArrayList<Integer> result = LongDivision.splitDivIntoDigits(12345);
        assertEquals(expectedDigits, result);
    }

    /**
     *  Test of method that perform intermediate calculates. (Divide
     *  current dividend and divisor).
     */
    @Test
    public void testDoIntermediateDivision() {
        int currentDividend = 14;
        LongDivision.divisor = 5;
        int expectedQuotient = 2;
        int expectedModulo = 4;
        int actualModulo = LongDivision.doIntermediateDivision(currentDividend);
        String error = "Fail in Test doIntermediateDivision: quotient incorrect.";
        assertEquals(error, LongDivision.quotient, expectedQuotient);
        error = "Fail in Test doIntermediateDivision: modulo incorrect.";
        assertEquals(error, expectedModulo, actualModulo);
    }

    /**
     *  Test of method that perform Division.
     */
    @Test
    public void testDoDivision()   {
        String error = "Fail in Test doDivision";
        ArrayList<Integer> expected = new ArrayList<Integer>(
                Arrays.asList(23, 20, 34, 30, 45, 40, 5)
        );
        LongDivision.divDigits.add(2);
        LongDivision.divDigits.add(3);
        LongDivision.divDigits.add(4);
        LongDivision.divDigits.add(5);
        LongDivision.divisor = 10;
        LongDivision.quotient =  0;
        LongDivision.doDivision();
        ArrayList<Integer> result = LongDivision.intermediateResults;
        assertEquals(error, expected, result);

    }

}
